local XMPQT = XLGetGlobal("XMPQT")

local _files = XMPQT.Helper.GetTestCaseTable()	

local dofiles = _files

if(_files) then --added By majianting
	XMPQT.Statistics = {ntotal = #_files,nerror=0,nsuccess=0} 
else 
	XMPQT.Statistics = {ntotal = 0,nerror=0,nsuccess=0} 
end

   
function UnLoadLuaModule(tFile)
------
	if(tFile) then--added By mjt
		for i = 1, #tFile do
			if(#tFile[i].file and #tFile[i].file > 0 ) then			
				local destPath = __document.."\\".."..\\..\\testcase\\"..tFile[i].file
				if(destPath and #tFile[i].file >0) then
					if (XLModuleExists(destPath)) then
						XLUnloadModule(destPath)
					end
				end
			end
		end
	end
end	

XMPQT.TreeIdMap = {}
XMPQT.IndexRunningMarke = {}
index = 1
XMPQT.tcIndex = 1

function InitVariable()	
	--XLMessageBox("InitVariable")
	XMPQT.tcIndex = 1
	XMPQT.TreeIdMap= {}
	index = 1
	
	XMPQT.IndexRunningMarke = {}
	
	
	if XMPQT.SelectTestCaseList ~= nil and #XMPQT.SelectTestCaseList > 0 then
		dofiles = XMPQT.SelectTestCaseList
	else
		dofiles = _files
	end
	------
	if(_files) then 
		XMPQT.Statistics = {ntotal = #_files,nerror=0,nsuccess=0} 
	else 
		XMPQT.Statistics = {ntotal = 0,nerror=0,nsuccess=0} 
	end

	
	UnLoadLuaModule(_files)
	
	loadmodule(1)
	
end

if XMPQT.QTEvent ~= nil then
	XMPQT.QTEvent:addEventListener("OnTestCaseStart",InitVariable)
end

function loadmodule(nIndex)
	if XMPQT.IndexRunningMarke[nIndex] == nil or XMPQT.IndexRunningMarke[nIndex] == false then
		XMPQT.IndexRunningMarke[nIndex] = true
		XMPQT.Output.Info2("======================================================",false)
		XMPQT.tcIndex = nIndex
		XMPQT.TreeIdMap[XMPQT.tcIndex] = 1
		local  destPath = nil
		local  testCaseType = "normal" 
	
		if (_files ~= nil) then--added by mjt 
			if XMPQT.SelectTestCaseList ~= nil and #XMPQT.SelectTestCaseList > 0 then
				XMPQT.tcIndex = tonumber(XMPQT.SelectTestCaseList[nIndex])
				XMPQT.TreeIdMap[XMPQT.tcIndex] = 1
				local i = tonumber(XMPQT.SelectTestCaseList[nIndex])
				if i >0 and i <= #_files then
					destPath = __document.."\\".."..\\..\\TestCase\\".._files[i].file
					testCaseType = _files[i].casetype or testCaseType
				end
				
			else
				if(#_files>0) then 
					destPath = __document.."\\".."..\\..\\TestCase\\".._files[nIndex].file
					testCaseType = _files[nIndex].casetype or testCaseType
				end
			end
				--XMPQT.Output.Info(destPath)
			if destPath  ~= nil then
				if testCaseType == "normal" then
					if (XLModuleExists(destPath)) then
						XLUnloadModule(destPath)
					
					end
					return XLLoadModule(destPath) 
				elseif testCaseType == "xmprt" then
					local command = "/start /path "..destPath
					XMPQT.XMPCommand(command)
					return true
				end
			end
		end
	end
end

function nextmodule(key, p1, p2)
	XMPQT.LOG("xmpqt_ nextmodule:"..index)
	XMPQT.InitTestCaseTable()
	dofiles=XMPQT.Helper.GetTestCaseTable()
	--if index <= 1 then return end
	if string.find(key,"xmpqt_") ~= nil and string.find(key,"__assert__") == nil then
		local preResult = 0
		if index+1 <= #dofiles then
			preResult = 1
		else
			preResult = 0
		end
		
		local ErrorInfo = nil
		
		local list = XMPQT.Helper.Split(key,":")
		info = list[#list]
		if #list and info ~= "nil" then
			sErrorInfo = "测试失败 : "..info
		else
			ErrorInfo = "测试失败"	
		end
		
		if string.find(key,"true") then
			XMPQT.Statistics.nsuccess = XMPQT.Statistics.nsuccess+1
			XMPQT.Output.Success("测试成功",preResult)
		elseif string.find(key,"false") then
			XMPQT.Statistics.nerror = XMPQT.Statistics.nerror+1
			XMPQT.Output.Error(ErrorInfo,preResult)
		end
		
		index = index+1
		if index <= #dofiles then
			if dofiles[index]["casetype"] == "xmprt" then
				loadmodule(index)
			else
				local ret,errobj = pcall(loadmodule,index)
				if not ret and string.find(errobj,XMPQT.AssertSignalString) ~= nil then
					if errobj ~= nil then
						nextmodule(errobj)
					end
				else
					XMPQT.Output.Error(errobj)
				end
			end
		else
			XMPQT.Output.Title("测试用例数量:"..XMPQT.Statistics.ntotal..",成功:"..XMPQT.Statistics.nsuccess..",失败:"..XMPQT.Statistics.nerror);
			XMPQT.SelectTestCaseList = {}
			if XMPQT.QTEvent ~= nil then
				XMPQT.QTEvent:dispatchEvent("OnTestCaseEnd",1,2)
			end	
			
		end
	elseif string.find(key,"xmpqt_") ~= nil and string.find(key,"__assert__") ~= nil then
		XMPQT.Continue(false)
	end

end

function OnCommandLine(key,p1,p2)
	if key == "CommandLine" then
		if p2["type"] == "callfunc" then
			if p2["name"] == "Output.Title" then
				XMPQT.Output.Title(p2["text"])
			end
			if p2["name"] == "Output.Info" then
				XMPQT.Output.Info(p2["text"],tonumber(p2["level"]))
			end
			if p2["name"] == "Output.Warning" then
				XMPQT.Output.Warning(p2["text"])
			end
			if p2["name"] == "Output.Error" then
				XMPQT.Output.Error(p2["text"],tonumber(p2["offset"]))
			end
			if p2["name"] == "Output.Success" then
				XMPQT.Output.Success(p2["text"],tonumber(p2["offset"]))
			end
			if p2["name"] == "Continue" then
				local actual = p2["actual"] == "true"
				AsynCall ( function() XMPQT.Continue(actual,p2["info"]) end )
			end
		end
	end
end

XMPQT.API:Call("AttachListener",OnCommandLine)
XMPQT.API:Call("AttachListener",nextmodule)













